來,今天我們來聊一下怎麼幫redux在測試時添加初始化的狀態資料,借用昨天的程式,並增加store的code.
// strore.js
import { createStore } from 'redux';
import reducer from './reducer';
const store = createStore(reducer);
export default store;
接下來借用一下昨天的部分.這部分我們就不要按照昨天的點選按鈕,這下我們就可以取得,初始化狀態下的元件
import { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Provider } from 'react-redux';
import store from './store';
import Avatar from '....';
import store from './store';
import reducer from './reducer';
test('should render initialState data in component', () => {
const store = createStore(reducer, {name: 'example'});
const { getByTestId, getByText } = render(
<Provider store={store}>
<Avatar />
</Provider>
);
expect(getByTestId('name')).toHaveTextContent('example')
})
晚點想到再補一點東西